package org.example.myleet.p1001;

import java.util.HashMap;

public class Solution {

    public int[] gridIllumination(int n, int[][] lamps, int[][] queries) {
        //一盏灯能照亮4种路径：（假设在坐标中，row=y，col=x）
        //行：y=t
        //列：x=t
        //撇（正斜线）：y=x+t => t=y-x
        //捺（反斜线）：y=-x+t => t=x+y
        //因此对每一盏灯做预处理可以记录到这几种属性的散列值中
        HashMap<Integer, Integer> rowLampCountMap = new HashMap<>();
        HashMap<Integer, Integer> colLampCountMap = new HashMap<>();
        HashMap<Integer, Integer> diagonalLampCountMap = new HashMap<>();
        HashMap<Integer, Integer> antiDiagonalLampCountMap = new HashMap<>();
        HashMap<Integer, HashMap<Integer, int[]>> rowColLampMap = new HashMap<>();
        for (int[] lamp : lamps) {
            HashMap<Integer, int[]> colLampMap = rowColLampMap.computeIfAbsent(lamp[0], key -> new HashMap<>());
            if (!colLampMap.containsKey(lamp[1])) {
                //不重复的灯才纳入散列表中
                colLampMap.put(lamp[1], lamp);
                int rowLampCount = rowLampCountMap.computeIfAbsent(lamp[0], key -> 0);
                ++rowLampCount;
                rowLampCountMap.put(lamp[0], rowLampCount);
                int colLampCount = colLampCountMap.computeIfAbsent(lamp[1], key -> 0);
                ++colLampCount;
                colLampCountMap.put(lamp[1], colLampCount);
                int diagonalLampCount = diagonalLampCountMap.computeIfAbsent(lamp[1] - lamp[0], key -> 0);
                ++diagonalLampCount;
                diagonalLampCountMap.put(lamp[1] - lamp[0], diagonalLampCount);
                int antiDiagonalCount = antiDiagonalLampCountMap.computeIfAbsent(lamp[0] + lamp[1], key -> 0);
                ++antiDiagonalCount;
                antiDiagonalLampCountMap.put(lamp[0] + lamp[1], antiDiagonalCount);
            }
        }
        int[] result = new int[queries.length];
        for (int idx = 0; idx < queries.length; ++idx) {
            int[] query = queries[idx];
            //检查同一行有没有灯
            Integer rowLampCount = rowLampCountMap.get(query[0]);
            if (null != rowLampCount && rowLampCount > 0) {
                result[idx] = 1;
            } else {
                //检查同一列有没有灯
                Integer colLampCount = colLampCountMap.get(query[1]);
                if (null != colLampCount && colLampCount > 0) {
                    result[idx] = 1;
                } else {
                    //检查同一正斜线有没有灯
                    Integer diagonalLampCount = diagonalLampCountMap.get(query[1] - query[0]);
                    if (null != diagonalLampCount && diagonalLampCount > 0) {
                        result[idx] = 1;
                    } else {
                        //检查同一反斜线有没有灯
                        Integer antiDiagonalLampCount = antiDiagonalLampCountMap.get(query[0] + query[1]);
                        if (null != antiDiagonalLampCount && antiDiagonalLampCount > 0) {
                            result[idx] = 1;
                        }
                    }
                }
            }
            //熄灭查询位置周围的灯
            for (int i = -1; i <= 1; ++i) {
                int row = query[0] + i;
                if (0 <= row && row < n) {
                    HashMap<Integer, int[]>colLampMap = rowColLampMap.get(row);
                    if (null != colLampMap && colLampMap.size() > 0) {
                        for (int j = -1; j <= 1; ++j) {
                            int col = query[1] + j;
                            if (0 <= col && col < n) {
                                int[] lamp = colLampMap.remove(col);
                                if (null != lamp) {
                                    rowLampCount = rowLampCountMap.get(lamp[0]);
                                    if (null != rowLampCount) {
                                        --rowLampCount;
                                        if (rowLampCount < 1) {
                                            rowLampCountMap.remove(lamp[0]);
                                        } else {
                                            rowLampCountMap.put(lamp[0], rowLampCount);
                                        }
                                    }
                                    Integer colLampCount = colLampCountMap.get(lamp[1]);
                                    if (null != colLampCount) {
                                        --colLampCount;
                                        if (colLampCount < 1) {
                                            colLampCountMap.remove(lamp[1]);
                                        } else {
                                            colLampCountMap.put(lamp[1], colLampCount);
                                        }
                                    }
                                    Integer diagonalLampCount = diagonalLampCountMap.get(lamp[1] - lamp[0]);
                                    if (null != diagonalLampCount) {
                                        --diagonalLampCount;
                                        if (diagonalLampCount < 1) {
                                            diagonalLampCountMap.remove(lamp[1] - lamp[0]);
                                        } else {
                                            diagonalLampCountMap.put(lamp[1] - lamp[0], diagonalLampCount);
                                        }
                                    }
                                    Integer antiDiagonalLampCount = antiDiagonalLampCountMap.get(lamp[0] + lamp[1]);
                                    if (null != antiDiagonalLampCount) {
                                        --antiDiagonalLampCount;
                                        if (antiDiagonalLampCount < 1) {
                                            antiDiagonalLampCountMap.remove(lamp[0] + lamp[1]);
                                        } else {
                                            antiDiagonalLampCountMap.put(lamp[0] + lamp[1], antiDiagonalLampCount);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return result;
    }
}
